package com.css.apps.task.admin.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.task.exec.DoJob;
import com.css.apps.task.model.TTask;
import com.css.core.configuration.Environment;
import com.css.db.hibernate.HibernateUtil;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Json;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;

@SuppressWarnings("serial")
public class UpdTimerTask extends AdminAction implements ModelDriven {
	
	private static Log log = LogFactory.getLog(UpdTimerTask.class);
	private TTask item = null;

	public Object getModel() {
		return item;
	}

	public UpdTimerTask() {
		item = new TTask();
	}

	public String adminGo() {
		if (StringHelper.isEmpty(item.getName()) || StringHelper.isEmpty(item.getExeClass())
				|| StringHelper.isEmpty(item.getExeCorn()) || StringHelper.isEmpty(item.getOpenFlag())
				|| StringHelper.isEmpty(item.getJobType())) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
			return Action.ERROR;
		}
		if ("2".equals(item.getJobType()) && StringHelper.isEmpty(item.getExePara())) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
			return Action.ERROR;
		}
		TransactionCache tx = null;
		try {
			TTask oldItem = QueryCache.get(TTask.class, item.getUuid());
			if (oldItem == null) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.recordnotfound"));
				return Action.ERROR;
			}
			if (!item.getName().equalsIgnoreCase(item.getName())) {
				Object uuid = new QueryCache("select a.uuid from TTask a where a.name=:name and a.uuid!=:uuid")
						.setParameter("name", item.getName()).setParameter("uuid", item.getUuid()).setMaxResults(1)
						.uniqueResult();
				if (uuid != null) {
					result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("sysmgrMsg.nameExist"));
					return Action.ERROR;
				}
			}
			oldItem.setName(item.getName());
			oldItem.setDescription(item.getDescription());
			oldItem.setExeClass(item.getExeClass());
			oldItem.setExePara(item.getExePara());
			oldItem.setExeCorn(item.getExeCorn());
			oldItem.setOpenFlag(item.getOpenFlag());
			oldItem.setJobType(item.getJobType());
			tx = new TransactionCache();
			tx.update(oldItem);
			tx.commit();
			new DoJob().editTask(item);// 对修改之后的任务，更新执行内容

			// 记录修改日志
			LogPart lp = new LogPart();
			lp.setOpObjType(TTask.class.getName());
			lp.setOpObjId(item.getUuid());
			lp.setRelObjType(TTask.class.getName());
			lp.setRelObjId(item.getUuid());
			lp.setOpType(LogConstant.LOG_TYPE_MODIFY);
			lp.setLogLevel(LogConstant.LOG_LEVEL_COMMON);
			lp.setLogData(Json.object2json(oldItem));
			lp.save();
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			if (tx != null)
				tx.rollback();
			result = Ajax.JSONResult(1, Messages.getString("systemMsg.dbFaild"));
			log.error(ex.getMessage());
			return Action.ERROR;
		} finally {
			HibernateUtil.closeSession();
			tx = null;
		}
	}

	public TTask getitem() {
		return item;
	}

	public void setitem(TTask item) {
		this.item = item;
	}
}